home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2c.lha / p4-1.2c / contrib_f / pi.f < prev    next >
Text File  |  1993-05-24  |  4KB  |  129 lines

  1.  
  2. c**********************************************************************
  3. c  This program calculates the value of pi, using numerical integration
  4. c  with parallel processing.  The user selects the number of points of
  5. c  integration.  By selecting more points you get more accurate results
  6. c  at the expense of additional computation
  7. c
  8. c  p4 calls have been used to promote portability of source code.
  9. c
  10. c   Each node: 
  11. c    1) receives the number of rectangles used in the approximation.
  12. c    2) calculates the areas of it's rectangles.
  13. c    3) Synchronizes for a global summation.
  14. c   Node 0 prints the result.
  15. c
  16. c  Constants:
  17. c    
  18. c    NODEPID     always zero for iPSC/860 nodes
  19. c    SIZETYPE    initial message to the cube
  20. c    ALLNODES    used to load all nodes in cube with a node process
  21. c    INTSIZ      four bytes for an integer
  22. c    DBLSIZ      eight bytes for double precision
  23. c
  24. c  Variables:
  25. c
  26. c    pi  the calculated result
  27. c    n   number of points of integration.  
  28. c    x           midpoint of each rectangle's interval
  29. c    f           function to integrate
  30. c    sum,pi      area of rectangles
  31. c    tmp         temporary scratch space for global summation
  32. c    i           do loop index
  33. c****************************************************************************
  34.  
  35.       include 'p4f.h'
  36.  
  37.       double precision  pi, pi25dt, h, sum, x, f, a, temp
  38.       integer allnodes,nodepid,sizetype,intsiz,dblsiz,anynode
  39.       integer n, mynod, nodes, i, retcode, recvlen, masternode
  40.       integer sumtype      
  41.       parameter(PI25DT = 3.141592653589793238462643d0)
  42. clw      parameter(INTSIZ=4,DBLSIZ=8,SIZETYPE=10,ALLNODES=-1,NODEPID=0)
  43. clw      parameter(ANYNODE=-1,SUMTYPE=17,MASTERNODE=0)
  44.       parameter(INTSIZ=4,DBLSIZ=8,ALLNODES=-1,NODEPID=0)
  45.       parameter(ANYNODE=-1,SUMTYPE=17)
  46.  
  47. c --  function to intergrate
  48.  
  49.       f(a) = 4.d0 / (1.d0 + a*a)
  50.  
  51. c --  use assignments, not parameters, for variables used in p4recv
  52.  
  53.       SIZETYPE=10
  54.       MASTERNODE=0
  55.       
  56.       call p4init()
  57.       call p4crpg()
  58.  
  59.       mynod = p4myid()
  60.       nodes = p4ntotids()
  61.  
  62. 10    if ( mynod .eq. 0 ) then
  63.          write(6,98)
  64. 98       format('Enter the number of intervals: (0 quits)')
  65.          read(5,99)n
  66. 99       format(i10)
  67.  
  68. clw --   this way to send to each individual node to
  69. clw --   distribute the work
  70.  
  71. clw         do i=1,nodes-1
  72. clw            call p4send(SIZETYPE,i,n,INTSIZ,retcode)
  73. clw         enddo
  74.  
  75. clw --   this way to let p4 send to all other nodes
  76.  
  77.          call p4brdcst(SIZETYPE,n,INTSIZ,retcode)
  78.       else
  79.    
  80. c --  Get the number of points of integration
  81.  
  82.          call p4recv(SIZETYPE, MASTERNODE, n, INTSIZ, 
  83.      >               recvlen, retcode)
  84.       endif
  85.  
  86. c --  everyone check for quit signal
  87.  
  88.       if ( n .le. 0 ) goto 30
  89.  
  90. c --  calculate the interval size
  91.  
  92.       h = 1.0d0/n
  93.  
  94.       sum  = 0.0d0
  95.       do 20 i = mynod+1, n, nodes
  96.          x = h * (dble(i) - 0.5d0)
  97.          sum = sum + f(x)
  98. 20    continue
  99.       pi = h * sum
  100.  
  101. c --  collect all the partial sums
  102.  
  103. clw --   first is the long way, each term with each send
  104.  
  105. clw      if (mynod .ne. 0) then
  106. clw         call p4sendr(SUMTYPE,MASTERNODE,pi,DBLSIZ,retcode)
  107. clw      else
  108. clw         do i=1,nodes-1
  109. clw            call p4recv(SUMTYPE,i,temp,DBLSIZ,recvlen,retcode)
  110. clw            pi = pi + temp
  111. clw         enddo
  112. clw      endif
  113.  
  114. clw --   this way uses a global sum operator to get pi
  115.  
  116.          call p4globop(SUMTYPE,pi,1,DBLSIZ,p4dblsumop,P4DBL,retcode)
  117.  
  118. c --  node 0 prints the answer.
  119.  
  120.       if (mynod .eq. 0) then
  121.          write(6, 97) pi, abs(pi - PI25DT)
  122.       endif
  123.       goto 10
  124.  
  125. 97    format('  pi is approximately: ', F18.16,'  Error is: ', F18.16)
  126. 30    call p4cleanup()
  127.       end
  128.  
  129.